package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.admin.dtos.SensitiveDto;
import com.heima.model.admin.pojos.AdSensitive;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.wemedia.mapper.WmSensitiveMapper;
import com.heima.wemedia.service.WmSensitiveService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
@Slf4j
@Transactional
public class WmSensitiveServiceImpl extends ServiceImpl<WmSensitiveMapper, WmSensitive> implements WmSensitiveService {
    /**
     * 查询敏感词
     * @param dto
     * @return
     */
    @Override
    public ResponseResult list(SensitiveDto dto) {
        // 1.参数检查
        if(dto == null){
            return ResponseResult.errorResult(400,"参数错误");
        }
        // 2 分页查询
        IPage pageCheck=new Page(dto.getPage(),dto.getSize());
        // 3 按照不同需求查询
        LambdaQueryWrapper<WmSensitive> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //3.1 关键字模糊匹配
        if(StringUtils.isNotBlank(dto.getName())){
            lambdaQueryWrapper.like(WmSensitive::getSensitives,dto.getName());
        }
        //3.2 排序
        lambdaQueryWrapper.orderByDesc(WmSensitive::getCreatedTime);
        pageCheck = page(pageCheck, lambdaQueryWrapper);
        //4. 返回结果
        ResponseResult responseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int) pageCheck.getTotal());
        responseResult.setData(pageCheck.getRecords());
        return responseResult;
    }

    /**
     * 增加敏感词
     * @param dto
     * @return
     */
    @Override
    public ResponseResult save(AdSensitive dto) {
        if(dto == null){
            return ResponseResult.errorResult(400,"参数错误");
        }
        WmSensitive wmSensitive = new WmSensitive();
        BeanUtils.copyProperties(dto,wmSensitive);
        wmSensitive.setCreatedTime(new Date());
        LambdaQueryWrapper<WmSensitive> queryWrapper = new LambdaQueryWrapper<WmSensitive>().eq(WmSensitive::getSensitives, wmSensitive.getSensitives());
         WmSensitive one = getOne(queryWrapper);
         if (one!=null)
         {
             return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
         }
        save(wmSensitive);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 修改敏感词
     * @param dto
     * @return
     */
    @Override
    public ResponseResult update(AdSensitive dto) {
        if(dto == null){
            return ResponseResult.errorResult(400,"参数错误");
        }
        WmSensitive wmSensitive = new WmSensitive();
        BeanUtils.copyProperties(dto,wmSensitive);
        updateById(wmSensitive);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 删除敏感词
     * @param id
     * @return
     */
    @Override
    public ResponseResult delete(Integer id) {
        if(id == null){
            return ResponseResult.errorResult(400,"参数错误");
        }
        removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


}